package com.frinder.quartz;

import org.quartz.*;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by kiktech on 2017/6/2.
 */
public class MyJob {

    static final ConcurrentHashMap<String, MyJob> JOBS = new ConcurrentHashMap<>();

    private Scheduler scheduler;

    private String jobName;

    private Class aClass;

    private String expression;

    public MyJob() {
    }

    public MyJob(Scheduler scheduler, String jobName, Class aClass, String expression) {
        this.scheduler = scheduler;
        this.jobName = jobName;
        this.aClass = aClass;
        this.expression = expression;
    }


    /**
     * 高度任务
     *
     * @throws SchedulerException
     */
    public void schedule() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(aClass).withIdentity(JobKey.jobKey(this.toString(), "job")).build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey(this.toString(), "job")).withSchedule(CronScheduleBuilder.cronSchedule(expression)).build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
        JOBS.put(jobName, this);
    }


    public Scheduler getScheduler() {
        return scheduler;
    }

    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public Class getaClass() {
        return aClass;
    }

    public void setaClass(Class aClass) {
        this.aClass = aClass;
    }

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }


    @Override
    public String toString() {
        return "MyJob{" +
                "jobName='" + jobName + '\'' +
                ", aClass=" + aClass +
                ", expression='" + expression + '\'' +
                '}';
    }
}
